#### Section 1, for Linux only. ####
# (problem exists)

cc_binary(
	name = "MyDll",
	srcs = ["MyDll/MyDll.cpp", "MyDll/iversion.h",
		"MyDll/MyDll.h"
		],

	copts = ["-fPIC"],
	visibility = ["//visibility:public"],
	linkshared = True,
)

# https://bazel.build/reference/be/c-cpp#cc_import
cc_import (
	name = "myImplib",
	hdrs = ["MyDll/MyDll.h"],
	deps = [":MyDll"], # but no use
	shared_library = "bazel-bin/libMyDll.so", # note: Cannot auto detect .so filename
)

cc_binary(
	name = "MyExeUseDll",
	srcs = ["MyExeUseDll/MyExeUseDll.cpp"],
	deps = [":MyDll", ":myImplib"],
	linkstatic = False,
	copts = ["-I MyDll"],
)

#### Section 2, for MSVC, all target names have _vc suffix. ####
# (problem exists)

cc_binary(
	name = "MyDll_vc",
	srcs = ["MyDll/MyDll.cpp", "MyDll/iversion.h",
		"MyDll/MyDll.h"
		],

	copts = ["/D DLLEXPORT_MyDll=__declspec(dllexport)"],
	visibility = ["//visibility:public"],
	linkshared = True,
)

cc_import (
	name = "myImplib_vc",
	hdrs = ["MyDll/MyDll.h"],
	interface_library = "MyDll_vc.if.lib", # tell the input implib
	shared_library = "MyDll_vc.dll",       # note: Cannot auto detect .dll filename
)

cc_binary(
	name = "MyExeUseDll_vc",
	srcs = ["MyExeUseDll/MyExeUseDll.cpp"],
	deps = [":myImplib_vc"],
	copts = ["-I MyDll"]

#	deps = ["MyDll_vc", ":myImplib_vc"],
#	linkstatic = True,
)


#### Section 3 , Generate DLL triggered by building EXE ####
# ( ok, as hinted by googletest //:gtest_samples )

config_setting(
    name = "windows",
    constraint_values = ["@platforms//os:windows"],
)

cc_library(
	name = "MyLibA",
	srcs = ["MyDll/MyDll.cpp", "MyDll/iversion.h",
		"MyDll/MyDll.h"
		],
    features = select({
        ":windows": ["windows_export_all_symbols"],
        "//conditions:default": [],
    }),
#	visibility = ["//visibility:public"],
)

cc_binary(
	name = "Test_MyLibA",
#	size = "small", # only for cc_test()
	srcs = ["MyExeUseDll/MyExeUseDll.cpp"],
	copts = ["-I MyDll"],
	
	linkstatic = False,
	deps = [":MyLibA"],
)

